package com.zhoun.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.zhoun.common.ServerResponse;
import com.zhoun.dao.CategoryMapper;
import com.zhoun.dao.ProductMapper;
import com.zhoun.pojo.Category;
import com.zhoun.pojo.Product;
import com.zhoun.service.ProductService;
import com.zhoun.vo.ProductDetailVo;
import com.zhoun.vo.ProductListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * 分页获取全部产品列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ServerResponse<PageInfo> getProductList(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.getProductList();
        List<ProductListVo> productListVoList = Lists.newArrayList();
        for (Product productItem : productList) {
            ProductListVo productListVo = assembleProductListVo(productItem);
            productListVoList.add(productListVo);
        }
        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 组装产品列表VO
     * @param product
     * @return
     */
    private ProductListVo assembleProductListVo(Product product){
        ProductListVo productListVo = new ProductListVo();

        productListVo.setCategoryName(categoryMapper.selectByPrimaryKey(product.getCategoryId()).getCategoryName());
        productListVo.setProductId(product.getProductId());
        productListVo.setProductName(product.getProductName());
        productListVo.setSpec(product.getSpec());
        productListVo.setHeatValue(product.getHeatValue());
        productListVo.setUnit(product.getUnit());
        productListVo.setPrice(product.getPrice());
        productListVo.setBrand(product.getBrand());
        productListVo.setRemark(product.getRemark());
        productListVo.setProject(product.getProject());
        productListVo.setStatus(product.getStatus());
        return productListVo;
    }

    /**
     * 新增或更新产品
     * @param product
     * @return
     */
    public ServerResponse addOrUpdate(Product product){
        if (product.getProductId() == null){
            if (productMapper.selectProductBySpec(product.getSpec()) != null){
                return ServerResponse.createByErrorMessage("产品已存在，请重新输入");
            }
            product.setStatus(0);
            int resultCount = productMapper.insert(product);
            if (resultCount == 0){
                return ServerResponse.createByErrorMessage("新增产品失败");
            }
            return ServerResponse.createBySuccess("新增产品成功",product);
        }else {
            if (productMapper.selectProductBySpec(product.getSpec()) != null){
                return ServerResponse.createByErrorMessage("产品已存在，请重新输入");
            }
            int resultCount = productMapper.updateByPrimaryKeySelective(product);
            if (resultCount == 0){
                return ServerResponse.createByErrorMessage("更新产品信息失败");
            }
            Product productNew = productMapper.selectByPrimaryKey(product.getProductId());
            return ServerResponse.createBySuccess("更新产品信息成功",productNew);
        }

    }

    /**
     * 删除产品
     * @param productId
     * @return
     */
    public ServerResponse delete(Integer productId){
        int resultCount = productMapper.deleteByPrimaryKey(productId);
        if (resultCount == 0){
            return ServerResponse.createByErrorMessage("删除产品失败");
        }
        return ServerResponse.createBySuccessMessage("删除产品成功");
    }


    /**
     * 通过型号查询产品
     * @param spec
     * @return
     */
    public ServerResponse selectProductBySpec(String spec){
        Product product = productMapper.selectProductBySpec(spec);
        if (product == null){
            return ServerResponse.createByErrorMessage("未查询到数据");
        }
        return ServerResponse.createBySuccess(product);
    }

    /**
     * 通过id查询产品
     * @param productId
     * @return
     */
    public ServerResponse getProductById(Integer productId){
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null){
            return ServerResponse.createByErrorMessage("未查询到数据");
        }
        ProductDetailVo productDetailVo = assembleDetailVo(product);
        return ServerResponse.createBySuccess(productDetailVo);
    }

    /**
     * 组装产品VO
     * @param product
     * @return
     */
    private ProductDetailVo assembleDetailVo(Product product){
        ProductDetailVo productDetailVo  = new ProductDetailVo();

        productDetailVo.setCategoryName(categoryMapper.selectByPrimaryKey(product.getCategoryId()).getCategoryName());
        productDetailVo.setProductId(product.getProductId());
        productDetailVo.setName(product.getProductName());
        productDetailVo.setSpec(product.getSpec());
        productDetailVo.setHeatValue(product.getHeatValue());
        productDetailVo.setUnit(product.getUnit());
        productDetailVo.setPrice(product.getPrice());
        productDetailVo.setBrand(product.getBrand());
        productDetailVo.setRemark(product.getRemark());
        productDetailVo.setProject(product.getProject());
        productDetailVo.setStatus(product.getStatus());
        return productDetailVo;
    }


    public ServerResponse getProductListByCategoryId(Integer categoryId){
        List<Product> productList = productMapper.getProductListByCategoryId(categoryId);
        return ServerResponse.createBySuccess(productList);
    }


}
